﻿<%@ Page Title="Improvement Action Management" Language="C#" MasterPageFile="~/Internal/Internal.Master" AutoEventWireup="true"
    CodeBehind="CorrectiveAction.aspx.cs" Inherits="Landscape.WebApp.Internal.Operations.CorrectiveAction" Theme="Internal" %>

<%@ Register src="../../Controls/ActionDetails.ascx" tagname="ActionDetails" tagprefix="uc1" %>

<%@ Register src="../../Controls/ActionResponse.ascx" tagname="ActionResponse" tagprefix="uc2" %>

<%@ Register src="../../Controls/ActionSearch.ascx" tagname="ActionSearch" tagprefix="uc3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript" src="../../Scripts/ModuleMethods/ActionTrackMgmt.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:HiddenField ID="hfActionID" runat="server" />
    <table style="height: 100%; width: 100%">
        <tr>
            <td style="height: 70px">
                <telerik:RadToolBar ID="rtbModuleActions" runat="server" Width="100%" OnClientButtonClicked="rtbModuleActions_OnClientButtonClicked"
                    EnableShadows="True">
                    <Items>
                        <telerik:RadToolBarButton runat="server" CommandName="LIST" ImageUrl="~/Images/Search.png"
                            Text="Search" CheckOnClick="True" ImagePosition="AboveText">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton runat="server" CommandName="NEW" ImageUrl="~/Images/Write.png"
                            Text="New" CheckOnClick="True" ImagePosition="AboveText">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton runat="server" CommandName="SAVE" ImageUrl="~/Images/Floppy.png"
                            Text="Save" CheckOnClick="True" ImagePosition="AboveText">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton runat="server" CommandName="DELETE" ImageUrl="~/Images/Delete_32x32.png"
                            Text="Delete" CheckOnClick="True" ImagePosition="AboveText">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton runat="server" IsSeparator="True" Text="Space">
                        </telerik:RadToolBarButton>
                        <telerik:RadToolBarButton runat="server" CommandName="RELEASE" ImageUrl="~/Images/Release.png"
                            Text="Finalize" CheckOnClick="True" ImagePosition="AboveText">
                        </telerik:RadToolBarButton>
                    </Items>
                </telerik:RadToolBar>
            </td>
        </tr>
        <tr>
            <td style="vertical-align: top; height: 30px">
                <telerik:RadTabStrip ID="rtsCAR" runat="server" MultiPageID="RadMultiPage1"
                    SelectedIndex="0" Width="100%" OnClientTabSelecting="rtsCAR_OnClientTabSelecting">
                    <Tabs>
                        <telerik:RadTab runat="server" Text="Search Results" PageViewID="RadPageView1">
                        </telerik:RadTab>
                        <telerik:RadTab runat="server" Text="Action Details" PageViewID="RadPageView2">
                        </telerik:RadTab>
                        <telerik:RadTab runat="server" Text="Response" PageViewID="RadPageView3">
                        </telerik:RadTab>
                    </Tabs>
                </telerik:RadTabStrip>
            </td>
        </tr>
        <tr>
            <td style="vertical-align: top; overflow: auto; vertical-align: top" class="SetMyHeight">
                <%--<div style="display: none">--%>
                <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="0" Height="100%"
                    Width="100%">
                    <telerik:RadPageView ID="RadPageView1" runat="server">
                        <uc3:ActionSearch ID="ActionSearch1" runat="server" />
                    </telerik:RadPageView>
                    <telerik:RadPageView ID="RadPageView2" runat="server">
                        <uc1:ActionDetails ID="ActionDetails1" runat="server" />
                    </telerik:RadPageView>
                    <telerik:RadPageView ID="RadPageView3" runat="server">
                        <uc2:ActionResponse ID="ActionResponse1" runat="server" />
                    </telerik:RadPageView>
                </telerik:RadMultiPage>
            </td>
        </tr>
    </table>
    <script type="text/javascript">
        function rtbModuleActions_OnClientButtonClicked(sender, args) {
            processCommands(args.get_item().get_commandName());
            args.get_item().set_checked(false);
        }

        function processCommands(pCmdName) {
            switch (pCmdName) {
                case 'SAVE':
                    {
                        var carMngr = initCARObject();
                        if (carMngr.GetActiveTabIndex() == 1) {
                            if (MASTER_GetCurrentBusinessObject() != null || carMngr.TitleCtrl.get_value().trim() != '') {
                                //carMngr.SaveCAR(MASTER_GetCurrentUserCode());
                                if (GLOBAL_CheckUserHasPrivilege('EDIT')) {
                                    carMngr.SaveCAR(MASTER_GetCurrentUserCode());
                                } else {
                                    N_NotifyMessage('Security', 'You don\'t have the permission to perfom this operation.', 3);
                                }
                            }
                            else {
                                N_NotifyMessage('Invalid', 'There is no active action item. Enter the title to save.', 3);
                            }
                        }

                        if (carMngr.GetActiveTabIndex() == 2) {
                            saveResponseCtrl();
                        }

                        break;
                    }

                case 'LIST':
                    {
                        if (GLOBAL_CheckUserHasPrivilege('SEARCH')) {
                            var carMngr = initCARObject();
                            carMngr.SearchActions();
                            carMngr.SelectTabByIndex(0);
                        } else {
                            N_NotifyMessage('Security', 'You don\'t have the permission to perfom this operation.', 3);
                        }

                        break;
                    }

                case 'DELETE':
                    {
                       /* if (MASTER_GetCurrentBusinessObject() != null) {
                            if (GLOBAL_CheckUserHasPrivilege('DELETE')) {
                                //performDelete();
                            } else {
                                N_NotifyMessage('Security', 'You don\'t have the permission to perfom this operation.', 3);
                            }
                        }
                        else {
                            N_NotifyMessage('Invalid', 'Please create or select the incident for save operation.', 3);
                        }*/
                        break;
                    }

                case 'NEW':
                    {
                        var carMngr = initCARObject();
                        if (carMngr.GetActiveTabIndex() == 1 || carMngr.GetActiveTabIndex() == 0) {
                            carMngr.ClearResponseCtrl();
                            carMngr.ClearDetailsCtrl();
                            carMngr.SelectTabByIndex(1);
                        }

                        if (carMngr.GetActiveTabIndex() == 2) {
                            carMngr.ClearResponseCtrl();
                        }

                        break;
                    }

                case 'PRINT':
                    {
                        /*if (MASTER_GetCurrentBusinessObject() != null) {
                            if (GLOBAL_CheckUserHasPrivilege('PRINT')) {
                                var erMngr = initERObject();
                                if (erMngr.GetActiveTabIndex() == 3) {
                                    //Feedback
                                    var url = "PrintFeedback.aspx?ICode=" + MASTER_GetCurrentBusinessObject().ID;
                                    window.open(url);
                                }
                            } else {
                                N_NotifyMessage('Security', 'You don\'t have the permission to perfom this operation.', 3);
                            }
                        }
                        else {
                            N_NotifyMessage('Invalid', 'Please create or select the incident for save operation.', 3);
                        }*/

                        break;
                    }

                case 'RELEASE':
                    {
                        var carMngr = initCARObject();
                        if (carMngr.GetActiveTabIndex() == 1) {
                            if (MASTER_GetCurrentBusinessObject() != null || carMngr.TitleCtrl.get_value().trim() != '') {
                                //carMngr.SaveCAR(MASTER_GetCurrentUserCode());
                                if (GLOBAL_CheckUserHasPrivilege('APPROVE')) {
                                    carMngr.ReleaseCAR(MASTER_GetCurrentUserCode());
                                } else {
                                    N_NotifyMessage('Security', 'You don\'t have the permission to perfom this operation.', 3);
                                }
                            }
                            else {
                                N_NotifyMessage('Invalid', 'Please create or select the incident for save operation.', 3);
                            }
                        }

                        if (carMngr.GetActiveTabIndex() == 2) {
                            finilizeResponse();
                        }
                        break;
                    }
            }
        }

        function initCARObject() {
            return initSearchCtrl(initDetails(initTabData()));
        }

        function initTabData() {
            var carObject = new ActionTrackMgmt();
            carObject.TabCtrl = $find('<%=rtsCAR.ClientID %>');
            carObject.ModuleActionsCtrl = $find('<%=rtbModuleActions.ClientID %>');

            return carObject;
        }

        function loadActionDetails() {
            var carObject = initCARObject();
            carObject.LoadActionObject($get('<%=hfActionID.ClientID %>').value);
            carObject.SelectTabByIndex(2);
        }
                
        function pageLoad() {
            if ($get('<%=hfActionID.ClientID %>').value.trim() != '') {
                loadActionDetails();
            }

            //Show
            ToggleModuleActions(0, true); //Search
            ToggleModuleActions(1, true); //New

            //Hide
            ToggleModuleActions(2, false); //Save
            ToggleModuleActions(3, false); //Delete
            ToggleModuleActions(5, false); //Finalize
        }

        function rtsCAR_OnClientTabSelecting(sender, args) {
            switch (args.get_tab().get_index()) {
                case 0: //Search tab
                    {
                        //Show
                        ToggleModuleActions(0, true); //Search
                        ToggleModuleActions(1, true); //New

                        //Hide
                        ToggleModuleActions(2, false); //Save
                        ToggleModuleActions(3, false); //Delete
                        ToggleModuleActions(5, false); //Finalize
                        break;
                    }

                case 1: //Details tab
                    {
                        //Show
                        ToggleModuleActions(0, true); //Search
                        ToggleModuleActions(1, true); //New
                        ToggleModuleActions(2, true); //Save
                        ToggleModuleActions(3, true); //Delete
                        ToggleModuleActions(5, true); //Finalize

                        //Hide
                        
                        

                        break;
                    }

                case 2: //Response tab
                    {
                        //Show
                        ToggleModuleActions(0, true); //Search                        
                        ToggleModuleActions(2, true); //Save
                        ToggleModuleActions(3, true); //Delete
                        ToggleModuleActions(5, true); //Finalize

                        //Hide
                        ToggleModuleActions(1, false); //New

                        break;
                    }
            }
        }

        function ToggleModuleActions(pActionIndex, pShow) {
            var moduleActionCtrl = $find('<%=rtbModuleActions.ClientID %>');
            moduleActionCtrl.get_allItems()[pActionIndex].set_visible(pShow);
        }
    </script>
</asp:Content>
